<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>ES接口测试</span>
        </div>
      </template>

      <!-- 保存设备表单 -->
      <el-form :model="deviceForm" label-width="120px" class="demo-form">
        <el-form-item label="设备ID">
          <el-input v-model="deviceForm.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="deviceForm.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="经度">
          <el-input-number v-model="deviceForm.longitude" :precision="6" :step="0.000001" />
        </el-form-item>
        <el-form-item label="纬度">
          <el-input-number v-model="deviceForm.latitude" :precision="6" :step="0.000001" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存设备</el-button>
          <el-button type="success" @click="handleBatchSave">批量保存</el-button>
          <el-button type="warning" @click="handleUpdate">更新设备</el-button>
          <el-button type="danger" @click="handleDelete">删除设备</el-button>
        </el-form-item>
      </el-form>

      <!-- 查询表单 -->
      <el-form :model="queryForm" label-width="120px" class="demo-form">
        <el-form-item label="经度">
          <el-input-number v-model="queryForm.longitude" :precision="6" :step="0.000001" />
        </el-form-item>
        <el-form-item label="纬度">
          <el-input-number v-model="queryForm.latitude" :precision="6" :step="0.000001" />
        </el-form-item>
        <el-form-item label="距离/半径">
          <el-input-number v-model="queryForm.distance" :min="1" :max="100" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleNearbyQuery">查询附近设备</el-button>
          <el-button type="success" @click="handleCircleQuery">查询圆形范围</el-button>
          <el-button type="info" @click="handleNearbyPageQuery">分页查询附近</el-button>
          <el-button type="warning" @click="handleCirclePageQuery">分页查询圆形</el-button>
        </el-form-item>
      </el-form>

      <!-- 结果展示 -->
      <el-card class="result-card">
        <template #header>
          <div class="card-header">
            <span>查询结果</span>
          </div>
        </template>
        <pre>{{ JSON.stringify(queryResult, null, 2) }}</pre>
      </el-card>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import type { DeviceEs } from '@/api/vending/es/types';
import {
  saveDevice,
  saveBatchDevice,
  updateDevice,
  deleteDevice,
  listNearbyDevices,
  listDevicesInCircle,
  pageNearbyDevices,
  pageDevicesInCircle
} from '@/api/vending/es';

// 设备表单
const deviceForm = reactive<DeviceEs>({
  deviceId: '',
  deviceName: '',
  longitude: 116.404,
  latitude: 39.915
});

// 查询表单
const queryForm = reactive({
  longitude: 116.404,
  latitude: 39.915,
  distance: 5
});

// 查询结果
const queryResult = ref<any>(null);

// 保存设备
const handleSave = async () => {
  try {
    const res = await saveDevice(deviceForm);
    ElMessage.success('保存成功');
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('保存失败');
  }
};

// 批量保存
const handleBatchSave = async () => {
  try {
    const devices = [deviceForm];
    const res = await saveBatchDevice(devices);
    ElMessage.success('批量保存成功');
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('批量保存失败');
  }
};

// 更新设备
const handleUpdate = async () => {
  try {
    const res = await updateDevice(deviceForm);
    ElMessage.success('更新成功');
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('更新失败');
  }
};

// 删除设备
const handleDelete = async () => {
  try {
    const res = await deleteDevice(deviceForm.deviceId);
    ElMessage.success('删除成功');
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('删除失败');
  }
};

// 查询附近设备
const handleNearbyQuery = async () => {
  try {
    const res = await listNearbyDevices(queryForm.longitude, queryForm.latitude, queryForm.distance);
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('查询失败');
  }
};

// 查询圆形范围
const handleCircleQuery = async () => {
  try {
    const res = await listDevicesInCircle(queryForm.longitude, queryForm.latitude, queryForm.distance);
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('查询失败');
  }
};

// 分页查询附近设备
const handleNearbyPageQuery = async () => {
  try {
    const res = await pageNearbyDevices(queryForm.longitude, queryForm.latitude, queryForm.distance, 1, 10);
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('查询失败');
  }
};

// 分页查询圆形范围
const handleCirclePageQuery = async () => {
  try {
    const res = await pageDevicesInCircle(queryForm.longitude, queryForm.latitude, queryForm.distance, 1, 10);
    queryResult.value = res;
  } catch (error) {
    ElMessage.error('查询失败');
  }
};
</script>

<style scoped>
.demo-form {
  margin-bottom: 20px;
}
.result-card {
  margin-top: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
pre {
  background-color: #f5f7fa;
  padding: 10px;
  border-radius: 4px;
  max-height: 400px;
  overflow: auto;
}
</style>
